#include <jni.h>
#include <sys/select.h>

JNIEXPORT jint JNICALL Java_com_hzb_myvpn_model_AndroidTunDev_waitReady(JNIEnv *env, jclass clazz, jint jfd, jint timeout) {
	fd_set rfds;
	fd_set efds;
	struct timeval tv;
    int fd = (int)jfd;
	FD_ZERO(&rfds);
	FD_ZERO(&efds);
	FD_SET(fd, &rfds);
	FD_SET(fd, &efds);
	tv.tv_sec = (int)timeout;
	tv.tv_usec = 0;
    
	int retval = select(fd + 1, &rfds, NULL, &efds, &tv);
	if (retval == -1) {
		return -1;
	} else if (retval == 0) {
		return 0;
	} else if FD_ISSET(fd, &rfds) {
		return 1;
	} else {
		return -1;
	}
}
